package com.tencent.elife.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Scroller;

/**
 * @Description: 滑动效果的tab切换控件
 * 1.必须有大余两个子布局，而且有顺序要求，第一个是固定不动的view，第二个是要滑动的view
 * 2.可以在XML文件中指定如下自定义字段： 
 * index:初始选中的tab序号 
 * durition:滑动动画的持续时间
 * @author zijianlu
 * @date 2012-6-12
 */
public class SlideTabBar extends FrameLayout {

    private View mSlideView;
    private FrameLayout mSlideFrame;
    private FrameLayout.LayoutParams mSlideViewLp;

    private int mCount = 0;
    private int mCurIndex = 0;
    private int mDutition = 350;

    private int mHeight = 0;
    private int mWidth = 0;

    private int mItemWidth = 0;
    private int mItemHeight = 0;

    private int mSlideViewHeight = 0;
    private int mSlideViewWidth = 0;
    private int mSlideLeft = 0;

    private Scroller mScroller;
    private boolean isSizeInited = false;

    private boolean isTopSlide = true;
    private boolean isBottomSlide = false;

    private Context mContext;

    public SlideTabBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mScroller = new Scroller(context);
        mCurIndex = attrs.getAttributeIntValue(null, "index", 0);

        mDutition = attrs.getAttributeIntValue(null, "durition", 500);
        if (mDutition < 0) {
            mDutition = 500;
        }

        isTopSlide = attrs.getAttributeBooleanValue(null, "topSlide", false);
        isBottomSlide = attrs.getAttributeBooleanValue(null, "bottomSlide",
            false);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mCount = getChildCount();
        if (mCount < 2) {
            throw new RuntimeException("must have 2 child star view or more~~");
        }

        // 入参检查
        if (mCurIndex < 0) {
            mCurIndex = 0;
        } else if (mCurIndex > mCount - 2) {
            mCurIndex = mCount - 2;
        }

        mSlideView = getChildAt(mCount - 1);
        mSlideViewLp = (FrameLayout.LayoutParams) mSlideView.getLayoutParams();

        this.removeView(mSlideView);
        mSlideFrame = new FrameLayout(mContext);
        mSlideFrame.addView(mSlideView);
        this.addView(mSlideFrame);

        for (int i = 0; i < mCount - 1; i++) {
            View child = getChildAt(i);
            child.setTag(i);
            child.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    int index = (Integer) v.getTag();
                    if (index != mCurIndex) {
                        int startPos = -(mCurIndex * mItemWidth + mSlideLeft);
                        int toXDelta = (mCurIndex - index) * mItemWidth;
                        mCurIndex = index;

                        mScroller.forceFinished(true);
                        mScroller.startScroll(startPos, 0, toXDelta, 0,
                            mDutition);
                        invalidate();

                        if (mOnTabSwitchListener != null) {
                            mOnTabSwitchListener.onTabSwitch(index);
                        }
                    }
                }
            });
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (!isSizeInited) {
            View child = getChildAt(0);
            mWidth = MeasureSpec.getSize(widthMeasureSpec);
            mItemWidth = mWidth / (mCount - 1);
            mItemHeight = child.getMeasuredHeight();
            mHeight = mItemHeight;

            // 如果xml布局文件里面指定slideView的宽度大于0，就使用这个宽度，否则算一个出来
            mSlideViewWidth = mSlideView.getMeasuredWidth();
            if (mSlideViewWidth > 0) {
                mSlideLeft = (mItemWidth - mSlideViewWidth) / 2;
            } else {
                // 滑动条距离左边距是 item长度的 1/8
                mSlideLeft = mItemWidth / 8;
                mSlideViewWidth = mItemWidth - mSlideLeft * 2;
            }
            mSlideViewHeight = mSlideView.getMeasuredHeight();

            // 上部或者底部显示滑动条的模式时
            if (isTopSlide || isBottomSlide) {
                mHeight += mSlideViewHeight;
            }

            mSlideViewLp.width = mSlideViewWidth;
            mSlideViewLp.height = mSlideViewHeight;
            mSlideView.setLayoutParams(mSlideViewLp);

            int initPos = -(mCurIndex * mItemWidth + mSlideLeft);
            mSlideFrame.scrollTo(initPos, 0);

            isSizeInited = true;
        }

        setMeasuredDimension(mWidth, mHeight);

        int count = mCount - 1;
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            int wspec = MeasureSpec.makeMeasureSpec(mItemWidth,
                MeasureSpec.EXACTLY);
            int hspec = MeasureSpec.makeMeasureSpec(mItemHeight,
                MeasureSpec.EXACTLY);

            child.measure(wspec, hspec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
        int bottom) {
        int count = mCount - 1;
        int vLeft = 0;
        if (!isTopSlide && !isBottomSlide) {
            for (int i = 0; i < count; i++) {
                View v = getChildAt(i);
                v.layout(vLeft, 0, vLeft + mItemWidth, mItemHeight);
                vLeft += mItemWidth;
            }
            mSlideFrame.layout(0, 0, mWidth, mHeight);
        } else if (isTopSlide) {
            mSlideFrame.layout(0, 0, mWidth, mSlideViewHeight);
            for (int i = 0; i < count; i++) {
                View v = getChildAt(i);
                v.layout(vLeft, mSlideViewHeight, vLeft + mItemWidth,
                    mSlideViewHeight + mItemHeight);
                vLeft += mItemWidth;
            }
        } else if (isBottomSlide) {
            for (int i = 0; i < count; i++) {
                View v = getChildAt(i);
                v.layout(vLeft, 0, vLeft + mItemWidth, mItemHeight);
                vLeft += mItemWidth;
            }
            mSlideFrame.layout(0, mItemHeight, mWidth, mItemHeight
                + mSlideViewHeight);
        }
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            int curPosX = mScroller.getCurrX();
            mSlideFrame.scrollTo(curPosX, 0);
            invalidate();
        }
    }

    private OnTabSwitchListener mOnTabSwitchListener = null;

    /** 设置事件响应监听 */
    public void setOnTabSwitchListener(OnTabSwitchListener listener) {
        mOnTabSwitchListener = listener;
    }

    public interface OnTabSwitchListener {
        /** 周围的星星点击响应 */
        public void onTabSwitch(int position);
    }
}